//
//  MYGCommunityRecommendCollectionViewCell.swift
//  MYGrow
//
//  Created by lance on 2024/2/29.
//

import UIKit

class MYGCommunityRecommendCollectionViewCell: UICollectionViewCell {
    // MARK: - Property
    var model: MYGCommunityRecommendCellModel? {
        didSet {
            guard let model = model else { return }
            
            selectImageView.image = UIImage(named: model.isSelected ? "Community/recommend_selected" : "Community/recommend_unselected")
            titleLabel.text = model.title
            backgroundImageView.sd_setImage(with: URL(string: model.icon), placeholderImage: placeholderImage)
        }
    }
    
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        contentView.addSubviews([
            backgroundImageView,
            selectImageView,
            titleLabel
        ])
        
        titleLabel.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(15)
        }
        
        backgroundImageView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.bottom.equalTo(titleLabel.snp.top).offset(-10)
            make.height.equalTo(backgroundImageView.snp.width)
        }
        
        selectImageView.snp.makeConstraints { make in
            make.top.right.equalToSuperview().inset(7.5)
            make.size.equalTo(CGSize(width: 15, height: 15))
        }
    }
    
    // MARK: - lazy load
    private lazy var backgroundImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.layerCornerRadius = 5
        return imageView
    }()
    
    private lazy var selectImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "Community/recommend_unselected"))
        imageView.contentMode = .scaleAspectFill
        return imageView
    }()
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(14)
        label.textAlignment = .center
        label.textColor = .rgba(99, 100, 101)
        label.text = "心外科"
        return label
    }()
}
